import Vue from 'vue'
import Api from './api'
import store from './store'
// import vueTooltip from 'vue-directive-tooltip'
// import 'vue-directive-tooltip/css/index.css'
import 'intersection-observer'
import $ from 'jquery'
import MoonLite from 'moonlite'
import dateFns from 'date-fns'
import EventBus from './event'
import './array.polyfill'
import echarts from 'echarts'
Vue.prototype.$echarts = echarts

// eslint-disable-next-line
import zh_cn from 'date-fns/locale/zh_cn'

Vue.use(EventBus)
Vue.use(MoonLite)
Vue.use(Api)
// Vue.use(vueTooltip)
window.Promise = require('es6-promise').Promise

Vue.prototype.store = store
Vue.prototype.admin = function (user) {
  // return false
  user = user || store.get('user')
  return user && user.admin
}

Vue.prototype.hasPromise = function (policy) {
  let userPolicy = store.get('userPolicy')
  let { resouce, action } = policy
  let resouceArr = []
  let actionArr = []
  let hasActionPolicy
  // let hasResoucePolicy
  typeof resouce === 'string' && resouceArr.push(resouce)
  Object.prototype.toString.call(resouce) === '[object Array]' && (resouceArr = resouce)

  typeof action === 'string' && actionArr.push(action)
  Object.prototype.toString.call(action) === '[object Array]' && (actionArr = action)

  let userPolicyActions = []
  let userPolicyResInfo = []
  userPolicy && userPolicy.length && userPolicy.forEach((policy, index) => {
    Object.assign(userPolicyActions, policy.actions)
    !userPolicyResInfo[policy.res_info] && userPolicyResInfo.push(policy.res_info)
  })

  for (let i = 0; i < userPolicyActions.length; i++) {
    for (let h = 0; h < resouceArr.length; h++) {
      if (userPolicyActions[i] === resouceArr[h]) {
        hasActionPolicy = true
      }
    }
  }
  for (let i = 0; i < userPolicyResInfo.length; i++) {
    for (let j = 0; j < resouceArr.length; j++) {
      let resInfo = userPolicyResInfo[i]
      if (resInfo[resInfo.length - 1] === '*') {
        resInfo = resInfo.replace(/\*$/, '')
        console.log(resInfo)
        // console.log(resouceArr[j])
        if (resouceArr[j].indexOf(resInfo) > -1) {
          hasActionPolicy = true
        }
      }
    }
  }
  if (hasActionPolicy && hasActionPolicy) {
    return true
  }
  if (!userPolicy) {
    return true
  }
}

Vue.component('loading', require('../components/plugins/loading').default)
Vue.component('error', require('../components/plugins/error').default)

Vue.filter('capitalize', function (value) {
  if (!value) {
    return ''
  }
  return value.replace(/^([a-z])(.*)/gi, function (b, c, d) {
    return c.toUpperCase() + d
  })
})

Vue.filter('fromNow', function (value) {
  if (!value) {
    return ''
  }
  if (typeof value === 'object') {
    value = value.$date
  }
  return dateFns.distanceInWordsToNow(value, {
    locale: zh_cn
  })
})

Vue.filter('moment', function (date, format) {
  if (!date) {
    return ''
  }
  if (typeof date === 'object' && date.$date) {
    date = date.$date
  }
  if (date < +new Date() / 10) {
    date = date * 1000
  }
  return dateFns.format(date, format)
})

Vue.filter('size', function (bytes) {
  var thresh = 1024
  if (Math.abs(bytes) < thresh) {
    return bytes + ' B'
  }
  var units = ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
  var u = -1
  do {
    bytes /= thresh
    ++u
  } while (Math.abs(bytes) >= thresh && u < units.length - 1)
  return bytes.toFixed(1) + ' ' + units[u]
})

Vue.directive('dropdown', {
  bind (el, binding, vnode) {
    let $toggler = $(el).find('.dropdown-toggle')
    let $parent = $toggler.parent()
    if ($toggler.length > 0) {
      if ($toggler.hasClass('hover') || $parent.hasClass('hover')) {
        $parent.hover(
          function (e) {
            e.preventDefault()
            e.stopPropagation()
            $parent.toggleClass('open', !$parent.hasClass('open'))
          },
          function (e) {
            e.preventDefault()
            e.stopPropagation()
            $parent.toggleClass('open', !$parent.hasClass('open'))
          }
        )
      } else {
        $toggler.click(function (e) {
          e.preventDefault()
          e.stopPropagation()
          $parent.toggleClass('open', !$parent.hasClass('open'))
        })
      }
      $(document).on('click', function (e) {
        let target = $parent.find(e.target)
        if (target.length === 0) {
          $parent.removeClass('open')
        }
      })
    }
  }
})

// eslint-disable-next-line
Array.prototype.$remove = function (val) {
  var index = this.indexOf(val)
  if (index >= 0) {
    this.splice(index, 1)
  }
}
